<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            var levelOrder = function (root) {
                if (!root) return [];
                let res = [];
                let queue = [root];
                while (queue.length) {
                    let cur = [];
                    let length = queue.length;
                    for (let i = 0; i < length; i++) {
                        let node = queue.shift();
                        cur.push(node.val);
                        node.left && queue.push(node.left);
                        node.right && queue.push(node.right);
                    }
                    res.push(cur);
                }
                return res;
            };
        </script>
    </body>
</html>
